package com.pm.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.pm.common.utils.DictUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pm.common.annotation.Log;
import com.pm.common.core.controller.BaseController;
import com.pm.common.core.domain.AjaxResult;
import com.pm.common.enums.BusinessType;
import com.pm.business.domain.EnterpriseCertificate;
import com.pm.business.service.IEnterpriseCertificateService;
import com.pm.common.utils.poi.ExcelUtil;
import com.pm.common.core.page.TableDataInfo;

/**
 * 企业证照Controller
 * 
 * @author zxl
 * @date 2022-05-07
 */
@RestController
@RequestMapping("/business/enterprise_certificate")
public class EnterpriseCertificateController extends BaseController
{
    @Autowired
    private IEnterpriseCertificateService enterpriseCertificateService;

    /**
     * 查询企业证照列表
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise_certificate:list')")
    @GetMapping("/list")
    public TableDataInfo list(EnterpriseCertificate enterpriseCertificate)
    {
        startPage();
        List<EnterpriseCertificate> list = enterpriseCertificateService.selectEnterpriseCertificateList(enterpriseCertificate);
        return getDataTable(list);
    }

    /**
     * 查询企业证照列表（小程序端）
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise_certificate:list')")
    @GetMapping("/appList")
    public TableDataInfo appList(EnterpriseCertificate enterpriseCertificate)
    {
        startPage();
        List<EnterpriseCertificate> list = enterpriseCertificateService.selectEnterpriseCertificateList(enterpriseCertificate);
        TableDataInfo result = getDataTable(list);
        // 取第一张图片
        if(list!=null){
            list.forEach(cc->{
                if(cc.getImage()!=null){
                    cc.setImage(cc.getImage().split(",")[0]);
                }
            });
        }
        return result;
    }

    /**
     * 导出企业证照列表
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise_certificate:export')")
    @Log(title = "企业证照", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EnterpriseCertificate enterpriseCertificate)
    {
        List<EnterpriseCertificate> list = enterpriseCertificateService.selectEnterpriseCertificateList(enterpriseCertificate);
        ExcelUtil<EnterpriseCertificate> util = new ExcelUtil<EnterpriseCertificate>(EnterpriseCertificate.class);
        util.exportExcel(response, list, "企业证照数据");
    }

    /**
     * 获取企业证照详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise_certificate:query')")
    @GetMapping(value = "/{certificateId}")
    public AjaxResult getInfo(@PathVariable("certificateId") Long certificateId)
    {
        return AjaxResult.success(enterpriseCertificateService.selectEnterpriseCertificateByCertificateId(certificateId));
    }
    @PreAuthorize("@ss.hasPermi('business:enterprise_certificate:query')")
    @GetMapping(value = "/getAppInfo/{certificateId}")
    public AjaxResult getAppInfo(@PathVariable("certificateId") Long certificateId)
    {
        EnterpriseCertificate enterpriseCertificate = enterpriseCertificateService.selectEnterpriseCertificateByCertificateId(certificateId);
        String certificateType = enterpriseCertificate.getCertificateType();
        if(certificateType!=null && certificateType.length()>0){
            enterpriseCertificate.setCertificateType(DictUtils.getDictLabel("pm_enterprise_certificate_type",certificateType));
        }
        return AjaxResult.success(enterpriseCertificate);
    }

    /**
     * 新增企业证照
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise_certificate:add')")
    @Log(title = "企业证照", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EnterpriseCertificate enterpriseCertificate)
    {
        return toAjax(enterpriseCertificateService.insertEnterpriseCertificate(enterpriseCertificate));
    }

    /**
     * 修改企业证照
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise_certificate:edit')")
    @Log(title = "企业证照", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EnterpriseCertificate enterpriseCertificate)
    {
        return toAjax(enterpriseCertificateService.updateEnterpriseCertificate(enterpriseCertificate));
    }

    /**
     * 删除企业证照
     */
    @PreAuthorize("@ss.hasPermi('business:enterprise_certificate:remove')")
    @Log(title = "企业证照", businessType = BusinessType.DELETE)
	@DeleteMapping("/{certificateIds}")
    public AjaxResult remove(@PathVariable Long[] certificateIds)
    {
        return toAjax(enterpriseCertificateService.deleteEnterpriseCertificateByCertificateIds(certificateIds));
    }
}
